package 归并排序;

import java.util.Arrays;

public class GuiBingSort {

    public static void main(String[] args) {

        int[] nums = {3, 3, 2, 0, 0, 1};
        sort(nums, 0, nums.length - 1);
        System.out.println(Arrays.toString(nums));

    }

    public static void sort(int[] arr, int L, int R) {

        //base case
        if (L == R) {
            return;
        }
        int mid = L + ((R - L) >> 1);
        //左边有序
        sort(arr, L, mid);
        //右边有序
        sort(arr, mid + 1, R);
        //比较合并
        merge(arr, L, mid, R);


    }

    private static void merge(int[] arr, int L, int mid, int R) {
        int[] help = new int[R - L + 1];
        int i = 0;
        int p1 = L;
        int p2 = mid + 1;
        while (p1 <= mid && p2 <= R) {
            help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++];
        }
        while (p1 <= mid) {
            help[i++] = arr[p1++];
        }
        while (p2 <= R) {
            help[i++] = arr[p2++];
        }
        for (int j = 0; j < help.length; j++) {
            arr[L + j] = help[j];
        }
    }
}
